//
//  Tongzhi_xiangqing_vc.m
//  ainkjj
//
//  Created by MAX on 2020/4/27.
//  Copyright © 2020 zhiyiLiu. All rights reserved.
//

#import "Tongzhi_xiangqing_vc.h"
#import "UserInform_getInformList.h"
#import "UserInform_getInformList_tvCell.h"
@interface Tongzhi_xiangqing_vc ()
<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)NSMutableDictionary *dataDict;
@property (nonatomic, assign)int refreshIndex;
@property (nonatomic, assign)NSInteger totalPage;
@end

@implementation Tongzhi_xiangqing_vc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.groupTableViewBackgroundColor;
    self.title = @"通知";
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    // tableView.separatorColor = UIColor.redColor;
    //tableView.rowHeight = 96;
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    self.tableView.mj_header=[MJRefreshNormalHeader  headerWithRefreshingTarget:self refreshingAction:@selector(req)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [self.tableView.mj_header beginRefreshing];
    [self.tableView registerNib:[UINib nibWithNibName:@"UserInform_getInformList_tvCell" bundle:nil] forCellReuseIdentifier:@"UserInform_getInformList_tvCell"];
    
    [self.tableView.mj_header beginRefreshing];
}

- (NSMutableArray *)dataArr{
    if(_dataArr == nil)
    {
        _dataArr = [[NSMutableArray alloc] init];
        
    }
    
    return _dataArr;
}
- (NSMutableDictionary *)dataDict{
    if(_dataDict == nil)
    {
        _dataDict = [[NSMutableDictionary alloc] init];
    }
    
    return _dataDict;
}

- (void)req{
    
    
    
    //  请求服务器，获取列表，刷新tv
    self.refreshIndex = 1;
    [self.tableView.mj_footer resetNoMoreData];
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:app_code forKey:@"appCode"];
    [dict setObject:AuthSignin_model.sharedInstance.data.token forKey:@"token"];
    [dict setObject:@(self.model.informType).stringValue forKey:@"informType"];
    
    [PPHTTPRequest req:@"java/app/userInform/getInformList" parameters:dict success:^(id response) {
        NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
        if ([error_code isEqualToString:@"0"]) {
            UserInform_getInformList *model = [UserInform_getInformList mj_objectWithKeyValues:response];
            
            if (!model.data || model.data.count == 0) {
                [self showEmptyViewWithImage:UIImageMake(@"notime_img") text:nil detailText:nil buttonTitle:nil buttonAction:NULL];
            }else{
                [self hideEmptyView];
//                self.dataArr = model.data.mutableCopy;
                
                for (UserInform_getInformListData *m in model.data) {
                    NSMutableArray *m_arr = [self.dataDict objectForKey:m.informDay];
                    if (!m_arr) {
                        m_arr = NSMutableArray.new;
                        [self.dataDict setObject:m_arr forKey:m.informDay];
                        [self.dataArr addObject:m.informDay];
                    }
                    [m_arr addObject:m];
                    
                }
                
                self.totalPage = model.pages;
            }
            
            
            
            
        }
        else{
            
            
            [QMUITips showError:response[@"error_msg"] ];
            
        }
        [self endMjRefresh];
        
    } failure:^(NSError *error) {
        [self endMjRefresh];
        
        [QMUITips showError:[error localizedDescription] ];
        
    }];
    
    
}
- (void)loadMoreData{
    self.refreshIndex++;
    
    if (self.refreshIndex > self.totalPage) {
        // 拿到当前的上拉刷新控件，变为没有更多数据的状态
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
        
    }else{
        
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:app_code forKey:@"appCode"];
        [dict setObject:AuthSignin_model.sharedInstance.data.token forKey:@"token"];
        [dict setObject:@(self.refreshIndex).stringValue forKey:@"page"];
        [dict setObject:@(self.model.informType).stringValue forKey:@"informType"];

        [PPHTTPRequest req:@"java/app/userInform/getInformList" parameters:dict success:^(id response) {
            NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
            if ([error_code isEqualToString:@"0"]) {
                UserInform_getInformList *model = [UserInform_getInformList mj_objectWithKeyValues:response];

                
                if (!model.data) {
                    [self showEmptyViewWithImage:UIImageMake(@"notime_img") text:nil detailText:nil buttonTitle:nil buttonAction:NULL];
                }else{
                    [self hideEmptyView];
//                    [self.dataArr addObjectsFromArray:model.data];
                    
                    for (UserInform_getInformListData *m in model.data) {
                        NSMutableArray *m_arr = [self.dataDict objectForKey:m.informDay];
                        if (!m_arr) {
                            m_arr = NSMutableArray.new;
                            [self.dataDict setObject:m_arr forKey:m.informDay];
                            [self.dataArr addObject:m.informDay];

                        }
                        [m_arr addObject:m];
                        
                    }
                }
                
                
                
                
            }
            else{
                
                
                [QMUITips showError:response[@"error_msg"] ];
                
            }
            [self endMjRefresh];
            
        } failure:^(NSError *error) {
            [self endMjRefresh];
            
            [QMUITips showError:[error localizedDescription] ];
            
        }];
        
    }
    
}
- (void)endMjRefresh{
    
    dispatch_async(dispatch_get_main_queue(), ^(void) {
        [self.tableView reloadData];
    });
    
    if (self.tableView.mj_footer.isRefreshing) {
        [self.tableView.mj_footer endRefreshing];
    }
    
    
    if (self.tableView.mj_header.isRefreshing) {
        [self.tableView.mj_header endRefreshing];
    }
    
    
}
#pragma mark - ================== table view data source delegate ==================

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = self.dataArr[section];
    NSMutableArray *m_arr = [self.dataDict objectForKey:key];
    return m_arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UserInform_getInformList_tvCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UserInform_getInformList_tvCell"];
    if (!cell) {
        cell = [[UserInform_getInformList_tvCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UserInform_getInformList_tvCell"];
    }
    NSString *key = self.dataArr[indexPath.section];
    NSMutableArray *m_arr = [self.dataDict objectForKey:key];
    UserInform_getInformListData *list = m_arr[indexPath.row];
        
    //        cell.textLabel.text = list.title;
    //    LzyChangeDetail *m = self.dataArr[ind    exPath.row];
    //    cell.model = m;
    cell.model = list;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    
//    Tongzhi_xiangqing_vc *vc = [Tongzhi_xiangqing_vc new];
//    //  列表进入详情，需要列表带一个标志到下一页，然后在下一页请求服务器获取需要的数据
//    UserInform_getTypeListData *model = self.dataArr[indexPath.row];
//    vc.model = model;
//    [self.navigationController pushViewController:vc animated:YES];
//    vc.navigationItem.title = @"消息详情";
//    vc.hidesBottomBarWhenPushed = YES;
    
    
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.dataArr[section];
}
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

{
    
    view.tintColor = [UIColor whiteColor];
    
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
    
    header.contentView.backgroundColor = UIColor.groupTableViewBackgroundColor;
    
    header.textLabel.textAlignment=NSTextAlignmentCenter;
    header.textLabel.font = [UIFont systemFontOfSize:14];
    [header.textLabel setTextColor:[UIColor colorNamed:@"153153153"]];
    
}
@end
